/**
 * Created by 郝栩彬 on 2017/7/10.
 */
var express = require('express');
var router = express.Router()
var responseData = null;
var User=require('../models/User');
var jluquery = require("../unit/jluquery")
var jluquery = new jluquery()

router.use(function (req,res,next) {
    responseData={
        code:200,
        message:'',
    }
    next();
});


//用户接口
//用户接口

router.post('/user/finduser',function (req,res,next) {
    var username = req.body.username;
    console.log(username)
    User.findOne({
        username:username
    }).then(function (userInfo) {
        if(userInfo){
            responseData.code=4;
            responseData.message='用户名已被注册';
            res.json(responseData);
            return;
        }
    })
})

//用户注册路由
router.post('/user/register',function (req,res,next) {
    var username = req.body.username;
    var password = req.body.password;
    var jluname = req.body.jluname;
    var jlupassword = req.body.jlupassword;
    var stuName = req.body.stuName;
    var stuId=req.body.studId;
    if(username==''){
        responseData.code = 1;
        responseData.message='用户名不能为空！';
        res.json(responseData);
        return;
    }
    if(password==''){
        responseData.code=2;
        responseData.message = '密码不能为空！';
        res.json(responseData);
        return;
    }
    // if(repassword!==password){
    //     responseData.code = 3;
    //     responseData.message = '两次密码不一致';
    //     res.json(responseData);
    //     return;
    // }
 User.findOne({
     username:username
 }).then(function (userInfo) {
     if(userInfo){
         responseData.code=4;
         responseData.message='用户名已被注册';
         res.json(responseData);
         return;
     }else {
         var user = new User({
             username:username,
             password:password,
             jluname:jluname,
             jlupassword:jlupassword,
             name:stuName,
             stuId:stuId
         });
         return user.save();
     }
 }).then(function (newUserInfo) {
     console.log(newUserInfo);
     responseData.message='注册成功！';
     res.json(responseData);
 })

});
//用户登入路由
router.post('/user/login',function (req,res,next) {
    var username = req.body.username
    var password = req.body.password
    if(username==''||password==''){
        responseData.code=1;
        responseData.message = '用户名密码不能为空！';
        res.json(responseData);
        return;
    }
    User.findOne({
        username:username,
        password:password
    }).then(function (userInfo) {
        if(!userInfo){
            responseData.code=2;
            responseData.message='用户名或密码错误';
            res.json(responseData);
            return;
        }else{
            responseData.message='登入成功';
            console.log(req.cookies)

            responseData.userInfo = {
                name:userInfo.name,
                head:userInfo.head
            }
            req.cookies.set('userInfo',JSON.stringify({
                _id:userInfo._id,
                username:userInfo.username,
                name:new Buffer(userInfo.name).toString("base64"),
                head:responseData.userInfo.head
            }))

            res.json(responseData);
            return;
        }
    }).catch((e)=>{
        console.log(e)


    })
})
//用户注销，置空COOKIES
router.get('/user/logout',function(req,res){
    req.cookies.set('userInfo',JSON.stringify({
        _id:null,
        username:null
    }));
    res.json(responseData);
    return;
});
//用户接口
//用户接口


//学生接口
//学生接口

//吉大学生成绩查询
router.post("/student/query/course",function (req,res,next) {
    var username = req.body.username;
    var password = req.body.password;
    console.log(username,password)
    jluquery.getUserCourse(username,password).then((course)=>{
        res.json(course)
    })
})
//吉大学生信息查询
router.post("/student/query/userinfo",function (req,res) {
   var jluname = req.body.username
    var jlupassword = req.body.password
    jluquery.getUserInfo(jluname,jlupassword).then((userInfo)=>{
        res.json(userInfo)
    })
})
//吉大学生课表查询
//吉大学生信息查询
router.post("/student/query/score",function (req,res) {
    var username = req.body.username;
    var password = req.body.password;
    var type = req.body.type
    jluquery.getUserScore(username,password,type).then((course)=>{
        res.json(course)
    })
})
//学生接口
//学生接口


//老师/管理员接口
//老师/管理员接口


//老师/管理员接口
//老师/管理员接口

module.exports = router